<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<!-- <el-form-item>
					<el-input v-model="filters.name" placeholder="设备IP"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary">查询</el-button>
				</el-form-item> -->
				<!-- <el-form-item v-show="my_permi.indexOf('settlementWeight.shipang') > -1 || my_permi.indexOf('admin') > -1">
					<el-button type="primary" @click="shiBang" v-show="is_shibang == '0'">开始试磅</el-button>
					<el-button type="primary" @click="shiBang" v-show="is_shibang == '1'">结束试磅</el-button>
				</el-form-item> -->
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="clientHeight" >
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="desc" label="描述">
			</el-table-column>
			<el-table-column prop="upper_limit" label="上限值" width="110">
			</el-table-column>
			<el-table-column prop="lower_limit" label="下限值" width="110">
			</el-table-column>
			<el-table-column prop="unit" label="单位">
			</el-table-column>
			<el-table-column label="操作" width="210">
				<template slot-scope="scope">
					<el-button v-show="my_permi.indexOf('settlementWeight.put') > -1 || my_permi.indexOf('admin') > -1" type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination
				layout="prev, pager, next"
				@current-change="handleCurrentChange"
				:total="total"
				style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="标识值">
					<el-input v-model="editForm.identity" readonly auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述">
					<el-input v-model="editForm.desc" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="上限值">
					<el-input v-model="editForm.upper_limit" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="下限值">
					<el-input v-model="editForm.lower_limit" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="单位">
					<el-input v-model="editForm.unit" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { systemConfig, systemConfigs, isShibang } from '@/api/api';
	import util from '@/common/js/util'
    import { mapState } from 'vuex'
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				tableList: [],
				total: 0,
				page: 1,
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					identity: '',
					desc: '',
					upper_limit: '',
					lower_limit: '',
					unit: '',
				},
				is_shibang: '',
			}
		},
        computed: {

            ...mapState(
                {
                  'my_permi': state => state.my_permi,
                  'clientHeight': state => state.clientHeight
                }
            )
        },
		methods: {
			router(path) {
				this.$router.push(path)
			},
			handleCurrentChange(val) {
				this.page = val;
				this.system_config();
			},
			//获取设备警报信息
			system_config() {
				this.listLoading = true;
				systemConfig(this.page).then((res) => {
					if(res.data.status.errCode == '10002'){
						sessionStorage.removeItem('token');
						this.$message({
							message: res.data.status.message,
							type: 'warning'
				        });
				        setTimeout(() =>{
				        	this.router({path: '/login'})
				        }, 1500);
						return;
					}
					let newList = [];
					for(var i in res.data.data.data){
						if(res.data.data.data[i].identity !== "trial_weight"){
							newList.push(res.data.data.data[i])
						}
					}
					this.total = res.data.data.total
					this.tableList = newList
					this.listLoading = false
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editId = row.id;
				this.editForm = {
					identity: row.identity,
					desc: row.desc,
					upper_limit: row.upper_limit,
					lower_limit: row.lower_limit,
					unit: row.unit,
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							para.start_time = (!para.start_time || para.start_time == '') ? '' : util.formatDate.format(new Date(para.start_time), 'yyyy-MM-dd');
							para.end_time = (!para.end_time || para.end_time == '') ? '' : util.formatDate.format(new Date(para.end_time), 'yyyy-MM-dd');
							systemConfigs(this.editId, 'PUT', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.editLoading = false;
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.system_config();
							}).catch((err) => {
								console.dir(err)
							});
						});
					}
				});
			},
			shiBang() {
				isShibang().then((res) => {
					console.log(res)
					if(res.data.status.errCode == '0'){
						location.reload()
					}
				}).catch((err) => {
					console.dir(err)
				})
			},
			systemconfig() {
				systemConfig().then((res) => {
					for(var i in res.data.data){
						if(res.data.data[i].identity == 'trial_weight'){
							this.is_shibang = res.data.data[i].deviation
						}
					}
				}).catch((err) => {
					console.log(err)
				})
			}
		},
		mounted() {
			this.system_config();
			this.systemconfig();
		}
	}

</script>

<style scoped>

</style>